package com.maoye.ossp.biz.statement.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.formula.functions.Sumproduct;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.statement.entity.StatementSaleDetail;
import com.maoye.ossp.biz.statement.entity.StatementSaleNote;
import com.maoye.ossp.biz.statement.entity.StatementSales;
import com.maoye.ossp.biz.statement.entity.StatementSalesErro;
import com.maoye.ossp.biz.statement.entity.StatementSelfrunError;
import com.maoye.ossp.biz.statement.service.StatementSaleDetailService;
import com.maoye.ossp.biz.statement.service.StatementSaleNoteService;
import com.maoye.ossp.biz.statement.service.StatementSalesErroService;
import com.maoye.ossp.biz.statement.service.StatementSalesService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.util.DateUtils;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.msg.entity.TodoMessage;
import com.maoye.ossp.sys.msg.handler.MsgStrategyContext;
/**
 * 销售对账异常单controller类
 * @author yanghao
 *
 */
@Controller
@RequestMapping(value = "biz/statement/statementSalesErro")
public class StatementSalesErroController extends BaseController{
	
	@Resource
	private StatementSalesErroService statementSalesErroService;
	@Resource
	private StatementSaleNoteService statementSaleNoteService;
	@Resource
	private StatementSalesService statementSalesService;
	@Resource
	private StatementSaleDetailService statementSaleDetailService;
	
	@Resource
	MsgStrategyContext context;
	/**
	 * 增加对账异常单
	 * @param statementSalesErro
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "add")
	public String add(StatementSales statementSales , Model model) {
		Map map = new HashMap();
		StatementSales sales = statementSalesService.get(statementSales.getId().toString());
		StatementSalesErro statementSalesErro = new StatementSalesErro();
		statementSalesErro.setSaleDate(sales.getSaleDate());
		statementSalesErro.setSaleStatementId(sales.getId());
		statementSalesErro.setSupplierNo(sales.getSupplierNo());
		statementSalesErro.setSupplierName(sales.getSupplierName());
		model.addAttribute("statementSalesErro",statementSalesErro);
		return "/biz/statement/statementSalesErro/add";
	}
	/**
	 * 保存对账异常单
	 * @param statementSalesErro
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value = "save")
	public String save(StatementSalesErro statementSalesErro, Model model,
			RedirectAttributes redirectAttributes) throws ParseException {
			statementSalesErro.setId(idGenerator.getUId());
			statementSalesErro.setIsConfirmed(BizStatementConstant.UNRECONCILIATION);
			statementSalesErro.setCreateTime(new Date());
			Map map1= new HashMap();
			map1.put("billNo", statementSalesErro.getSaleNo());
			StatementSaleNote statementSaleNote = statementSaleNoteService.getSaleNotesByBillNo(map1);
			if(statementSaleNote !=null){
				statementSalesErro.setSaleDate(statementSaleNote.getSaleDate());
			}
			statementSalesErroService.create(statementSalesErro);
			
		addMessage(redirectAttributes, "保存异常单成功");
		return "redirect:/biz/statement/statementSales/list";
	}
	/**
	 * ajax异步获取数据
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	@RequestMapping(value = "ajax")
	public void ajax(StatementSalesErro statementSalesErro,HttpServletRequest request,HttpServletResponse response, Model model) throws Exception{
		StringBuilder jsonStr = new StringBuilder();
		response.setCharacterEncoding("utf-8");
		String contextPath=request.getContextPath();
		PrintWriter out = response.getWriter();
		try {
			Map map = new HashMap();
			map.put("barCode", statementSalesErro.getBarCode());
			map.put("billNo", statementSalesErro.getSaleNo());
			StatementSaleDetail statementSaleDetail =statementSaleDetailService.getBySnAndBc(map);
			if(statementSaleDetail !=null){
				jsonStr.append("{");
				jsonStr.append("success:true ,data:{");
				jsonStr.append("\"price\":");
				jsonStr.append(statementSaleDetail.getPrice());
				jsonStr.append(",");
				jsonStr.append("\"quantity\":");
				jsonStr.append(statementSaleDetail.getQuantity());
				jsonStr.append(",");
				jsonStr.append("\"amount\":");
				jsonStr.append(statementSaleDetail.getAmount());
				jsonStr.append("}");
				jsonStr.append("}");
			}else{
				jsonStr.append("{");
				jsonStr.append("success:false");
				jsonStr.append("}");
			}
				
			
		} catch (Exception e) {
			// TODO: handle exception
			jsonStr.append("{");
			jsonStr.append("success:false");
			jsonStr.append("}");
		}
		
		out.print(jsonStr.toString());
		out.flush();
		out.close();
	}
	/**
	 * 按异常提交日期、销售日期、供应商、异常是否确认查询异常单
	 * @param statementSalesErro
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	@RequestMapping(value = "list")
	public String list(StatementSalesErro statementSalesErro, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map map = new HashMap();
		map.put("isConfirmed", statementSalesErro.getIsConfirmed());
		Date date = statementSalesErro.getCreateTime();
		if(date!=null){
			DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			String createTime =  f.format(date);
			map.put("createTime", createTime);
		}
		map.put("saleDate", statementSalesErro.getSaleDate());
		map.put("supplierNo", statementSalesErro.getSupplierNo());
		List<StatementSalesErro> list = statementSalesErroService.getAll(map);
		model.addAttribute("list", list);
		return "/biz/statement/statementSalesErro/list";
	}
	@RequestMapping(value = "erroScheme")
	public String erroScheme(StatementSalesErro statementSalesErro, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		StatementSalesErro salesErro = statementSalesErroService.get(statementSalesErro.getId().toString());
		model.addAttribute("salesErro", salesErro);
		return "/biz/statement/statementSalesErro/erroScheme_add";
	}
	/**
	 * 保存异常处理方案并发送通知到供应商
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "saveErroScheme")
	public String saveErroScheme( StatementSalesErro statementSalesErro,
			HttpServletResponse response, Model model) {
		statementSalesErro.setIsConfirmed(BizStatementConstant.ISCONFIRMED);
		try {
			statementSalesErroService.update(statementSalesErro);
			//发送通知
			TodoMessage todo = new TodoMessage();
			context.send(todo);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return "redirect:/biz/statement/statementSalesErro/list";
	}
	/**
	 * 展示销售异常单详细信息
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "view")
	public String view( StatementSalesErro error,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		StatementSalesErro statementSalesErro = statementSalesErroService.get(error.getId().toString());
		model.addAttribute("statementSalesErro", statementSalesErro);
		return "/biz/statement/statementSalesErro/view";
	}
	/**
	 * 通知供应商异常不存在
	 * @param statementSelfrunError
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @author tf
	 * @data 2014年3月22日
	 */
	@RequestMapping(value = "exceptionError")
	public void exceptionError(StatementSalesErro statementSalesError ,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException  {
		StringBuilder jsonStr = new StringBuilder();
		response.setCharacterEncoding("utf-8");
		String contextPath=request.getContextPath();
		PrintWriter out = response.getWriter();
		try {
			StatementSalesErro error = statementSalesErroService.get(statementSalesError.getId().toString());
			//发送通知
			TodoMessage todo = new TodoMessage();
			context.send(todo);
			jsonStr.append("{success:true}");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			jsonStr.append("{success:false}");
		}	
		out.print(jsonStr.toString());
		out.flush();
		out.close();
	}
	
	public StatementSalesErroService getStatementSalesErroService() {
		return statementSalesErroService;
	}
	public void setStatementSalesErroService(
			StatementSalesErroService statementSalesErroService) {
		this.statementSalesErroService = statementSalesErroService;
	}
	public StatementSaleNoteService getStatementSaleNoteService() {
		return statementSaleNoteService;
	}
	public void setStatementSaleNoteService(
			StatementSaleNoteService statementSaleNoteService) {
		this.statementSaleNoteService = statementSaleNoteService;
	}
	public StatementSalesService getStatementSalesService() {
		return statementSalesService;
	}
	public void setStatementSalesService(StatementSalesService statementSalesService) {
		this.statementSalesService = statementSalesService;
	}
	public StatementSaleDetailService getStatementSaleDetailService() {
		return statementSaleDetailService;
	}
	public void setStatementSaleDetailService(
			StatementSaleDetailService statementSaleDetailService) {
		this.statementSaleDetailService = statementSaleDetailService;
	}
}
